package com.doitedu.day03.funcs

import scala.collection.{immutable, mutable}
import scala.io.Source

/**
 * @Date 22.6.22
 * @Created by HANGGE
 * @Description
 */
object _06FlattenMap方法02 {
  def main(args: Array[String]): Unit = {

    val lines = Source.fromFile("E://words.txt").getLines()
    val words = lines.map(line => {
      line.split("\\s+")
    }).toList
      .flatten
      .map(w=>w.toLowerCase)

    // 统计单词的个数
    // 单词 总次数
    val mp = new mutable.HashMap[String, Int]()
    // hello hello  hello  jim
    words.foreach(word=>{
      var cnt = mp.getOrElse(word, 0)
      cnt+=1
      mp.put(word ,cnt)  // hello 3   jim 1
    })

   //mp.toList.sortBy(-_._2).take(3).foreach(println)
   mp.toList.sortBy(-_._2).slice(0,3).foreach(println)

  }
}
